<?php
// 本类由系统自动生成，仅供测试用途
class CapitalSortAction extends BaseLoginAction {
   function __construct() {
       parent::__construct();
       
   }
    public function index(){
        $this->redirect("lists");
    }
    
    public function lists(){
        $sort=D("CapitalSort");
        $where=array(
            "mid"=>  $this->uid,
            C('DB_PREFIX')."capital_sort.fsid"=>0,
            "place"=>array("in",array("1","0"))
            );
        $page=  page(10, $sort, $where);
        $this->assign("page",$page);
        $list=$sort
                ->join("(select sum(number) as allnumber,fsid from ".C('DB_PREFIX')."capital_sort group by fsid) fsort ON fsort.fsid=".C('DB_PREFIX')."capital_sort.sid")
                ->field(C('DB_PREFIX')."capital_sort.*,fsort.allnumber")
                ->where($where)
                ->limit("{$page['start']},{$page['number']}")
                ->select();
        //echo $sort->getLastSql();
        $this->assign("list",$list);
        $this->display();
    }
    public function expendList(){
        $sort=D("CapitalSort");
        $where=array(
            "mid"=>  $this->uid,
            C('DB_PREFIX')."capital_sort.fsid"=>0,
            "place"=>array("in",array("2"))
            );
        $page=  page(10, $sort, $where);
        $this->assign("page",$page);
        $list=$sort
                ->where($where)
                ->join("(select sum(number) as allnumber,fsid from ".C('DB_PREFIX')."capital_sort group by fsid) fsort ON fsort.fsid=".C('DB_PREFIX')."capital_sort.sid")
                ->field(C('DB_PREFIX')."capital_sort.*,fsort.allnumber")
                ->limit("{$page['start']},{$page['number']}")
                ->select();
        $this->assign("list",$list);
        $this->display();
    }

    public function son(){
        if(isset($_GET['id'])&&  intval($_GET['id'])>0){
            $id=intval($_GET['id']);
            $sort=D("CapitalSort");
            $where=Array(
                "fsid"=>$id
            );
            $list=  $sort
                    ->where($where)->select();
            $this->assign("list",$list);
            $capitalsort=$sort->where(array('sid'=>$id))->find();
            $this->assign("sort",$capitalsort);
            $this->display();
        }else{
            $this->error("数据错误");
        }
        
    }
    public function add(){
        $sort=D("CapitalSort");
        $where=array(
            "fsid"=>0,
            "mid"=>  $this->uid,
            "place"=>array("in",array("0","1"))
        );
        $list=$sort->where($where)->select();
        $this->assign("list",$list);
        $member=D("CapitalSortMember");
        $where=Array(
            "mid"=>  $this->uid
        );
        $member_list=$member->where($where)->select();
        $this->assign("member",$member_list);
        
        $this->display();
    }
    public function addExpendSort(){
        $sort=D("CapitalSort");
        $where=array(
            "fsid"=>0,
            "mid"=>  $this->uid,
            "place"=>array("in",array("2"))
        );
        $list=$sort->where($where)->select();
        $this->assign("list",$list);
        $member=D("CapitalSortMember");
        $where=Array(
            "mid"=>  $this->uid
        );
        $member_list=$member->where($where)->select();
        $this->assign("member",$member_list);
        $this->display();
    }
    public function doadd(){
        if(isset($_POST['name'])&&  $_POST['name']!=''){
            $insert_arr=Array(
                "mid"=>  $this->uid,
                "name"=>$_POST['name'],
                "status"=>1,
                "place"=>$_POST['place'],
                "fsid"=>$_POST['fsid'],
                "smid"=>$_POST['smid']
            );
            $sort=D("CapitalSort");
            $insert=$sort->data($insert_arr)->add();
            if($insert){
                $this->redirect("index");
            }else{
                $this->error("添加失败");
            }
        }
    }
    public function edit(){
        if(isset($_GET['id'])&&  intval($_GET['id'])>0){
            $id=intval($_GET['id']);
            $sort=D("CapitalSort");
            $where=Array(
                "sid"=>$id,
                "mid"=>  $this->uid
            );
            $capitalsort=$sort->where($where)->find();
            if($capitalsort){
                $this->assign("sort",$capitalsort);
                
                $where=Array(
                    "fsid"=>0,
                    "mid"=>  $this->uid
                );
                $list=$sort->where($where)->select();
                $this->assign("list",$list);
                $member=D("CapitalSortMember");
                $where=Array(
                    "mid"=>  $this->uid
                );
                $member_list=$member->where($where)->select();
                $this->assign("member",$member_list);
                if($capitalsort['place']==2){
                    $this->display("EditExpendSort");
                }else{
                    $this->display();
                }
            }else{
                $this->error("数据错误");
            }
        }  else {
            $this->error("数据错误");
        }
    }
    public function doedit(){
        if(isset($_POST['name'])&&  $_POST['name']!=''
                &&isset($_GET['id'])&&  intval($_GET['id'])>0){
            $id=  intval($_GET['id']);
            $update_arr=Array(
                "name"=>$_POST['name'],
                "place"=>$_POST['place'],
                "fsid"=>$_POST['fsid'],
                "smid"=>$_POST['smid']
            );
            $where=Array(
                "mid"=>  $this->uid,
                "sid"=>$id
            );
            $sort=D("CapitalSort");
            $update=$sort->where($where)->data($update_arr)->save();
            
            if($update!==false){
                $this->redirect("index");
            }else{
                $this->error("修改失败");
            }
        }else{
            $this->error("数据错误");
        }
    }
}